
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>逐行解读reactive如何构建对象和数组的响应式对象 · petite-vue源码剖析</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="^_^肥仔John">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-donate/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="2_reactive_map_set.html" />
    
    
    <link rel="prev" href="0_intro.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">前言</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    《Petite-Vue源码剖析》简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../0_intro/1_reason_for_source_reading.html">
            
                <a href="../0_intro/1_reason_for_source_reading.html">
            
                    
                    为什么要读源码
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">在线渲染</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../1_online_rendering/0_intro.html">
            
                <a href="../1_online_rendering/0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../1_online_rendering/1_start_from_static_view.html">
            
                <a href="../1_online_rendering/1_start_from_static_view.html">
            
                    
                    从静态视图首次渲染开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../1_online_rendering/2_initial_rendering_and_rerendering_for_v-if.html">
            
                <a href="../1_online_rendering/2_initial_rendering_and_rerendering_for_v-if.html">
            
                    
                    条件v-if的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../1_online_rendering/3_initial_rendering_and_rerendering_for_v-for.html">
            
                <a href="../1_online_rendering/3_initial_rendering_and_rerendering_for_v-for.html">
            
                    
                    循环v-for的首次渲染和重新渲染
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../1_online_rendering/4_directives/1_attribute_binding.html">
            
                <a href="../1_online_rendering/4_directives/1_attribute_binding.html">
            
                    
                    属性绑定v-bind的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../1_online_rendering/4_directives/2_event_binding.html">
            
                <a href="../1_online_rendering/4_directives/2_event_binding.html">
            
                    
                    事件绑定v-on的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../1_online_rendering/4_directives/3_bidirectional_binding.html">
            
                <a href="../1_online_rendering/4_directives/3_bidirectional_binding.html">
            
                    
                    双向绑定v-model的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../1_online_rendering/4_directives/4_ref.html">
            
                <a href="../1_online_rendering/4_directives/4_ref.html">
            
                    
                    特殊指令ref的工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../1_online_rendering/5_tips_for_template.html">
            
                <a href="../1_online_rendering/5_tips_for_template.html">
            
                    
                    优化手段template的原理和使用技巧
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">响应式系统</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="0_intro.html">
            
                <a href="0_intro.html">
            
                    
                    章节介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.2" data-path="1_reactive_object_array.html">
            
                <a href="1_reactive_object_array.html">
            
                    
                    逐行解读reactive如何构建对象和数组的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="2_reactive_map_set.html">
            
                <a href="2_reactive_map_set.html">
            
                    
                    逐行解读reactive如何构建Map和Set的响应式对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="3_register_side_effect.html">
            
                <a href="3_register_side_effect.html">
            
                    
                    逐行解读effect如何注册副作用函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="4_cleanup_algorithm.html">
            
                <a href="4_cleanup_algorithm.html">
            
                    
                    依赖清理优化算法详解-算法思路
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="5_smi.html">
            
                <a href="5_smi.html">
            
                    
                    依赖清理优化算法详解-基于JavaScript引擎的SMI进一步压榨性能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="6_trigger_and_scheduler.html">
            
                <a href="6_trigger_and_scheduler.html">
            
                    
                    逐行解读trigger和调度器
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">沙箱模型</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../3_sandbox.html">
            
                <a href="../3_sandbox.html">
            
                    
                    模板表达式的沙箱模型
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >逐行解读reactive如何构建对象和数组的响应式对象</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x7EC4;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;"><b>1. </b>&#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x7EC4;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;</a></li><ul><li><span class="title-icon "></span><a href="#&#x4E00;&#x5207;&#x6E90;&#x4E8E;proxy"><b>1.1. </b>&#x4E00;&#x5207;&#x6E90;&#x4E8E;Proxy</a></li><ul><li><span class="title-icon "></span><a href="#&#x54CD;&#x5E94;&#x5F0F;&#x7F16;&#x7A0B;"><b>1.1.1. </b>&#x54CD;&#x5E94;&#x5F0F;&#x7F16;&#x7A0B;</a></li></ul><li><span class="title-icon "></span><a href="#&#x6DF1;&#x5165;reactive&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;"><b>1.2. </b>&#x6DF1;&#x5165;reactive&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;</a></li><ul><li><span class="title-icon "></span><a href="#&#x6E90;&#x7801;&#x89E3;&#x8BFB;&#x2014;&#x2014;reactive&#x5165;&#x53E3;"><b>1.2.1. </b>&#x6E90;&#x7801;&#x89E3;&#x8BFB;&#x2014;&#x2014;reactive&#x5165;&#x53E3;</a></li><li><span class="title-icon "></span><a href="#&#x6E90;&#x7801;&#x89E3;&#x8BFB;-&#x4EE3;&#x7406;object&#x548C;array&#x7684;basehandlers"><b>1.2.2. </b>&#x6E90;&#x7801;&#x89E3;&#x8BFB;-&#x4EE3;&#x7406;Object&#x548C;Array&#x7684;baseHandlers</a></li><li><span class="title-icon "></span><a href="#&#x62E6;&#x622A;&#x5220;&#x9664;&#x64CD;&#x4F5C;"><b>1.2.3. </b>&#x62E6;&#x622A;&#x5220;&#x9664;&#x64CD;&#x4F5C;</a></li><li><span class="title-icon "></span><a href="#&#x62E6;&#x622A;&#x68C0;&#x67E5;&#x5B58;&#x5728;&#x4E0E;&#x5426;&#x64CD;&#x4F5C;name-in-state"><b>1.2.4. </b>&#x62E6;&#x622A;&#x68C0;&#x67E5;&#x5B58;&#x5728;&#x4E0E;&#x5426;&#x64CD;&#x4F5C;(&apos;name&apos; in state)</a></li><li><span class="title-icon "></span><a href="#&#x62E6;&#x622A;&#x952E;&#x904D;&#x5386;&#x64CD;&#x4F5C;"><b>1.2.5. </b>&#x62E6;&#x622A;&#x952E;&#x904D;&#x5386;&#x64CD;&#x4F5C;</a></li></ul><li><span class="title-icon "></span><a href="#proxy&#x4E2D;&#x7684;receiver&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><b>1.3. </b>Proxy&#x4E2D;&#x7684;receiver&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</a></li><ul><li><span class="title-icon "></span><a href="#&#x95EE;&#x9898;1&#x7684;&#x89E3;&#x6790;"><b>1.3.1. </b>&#x95EE;&#x9898;1&#x7684;&#x89E3;&#x6790;</a></li><li><span class="title-icon "></span><a href="#&#x95EE;&#x9898;2&#x7684;&#x89E3;&#x6790;"><b>1.3.2. </b>&#x95EE;&#x9898;2&#x7684;&#x89E3;&#x6790;</a></li><li><span class="title-icon "></span><a href="#&#x95EE;&#x9898;3&#x7684;&#x89E3;&#x6790;"><b>1.3.3. </b>&#x95EE;&#x9898;3&#x7684;&#x89E3;&#x6790;</a></li></ul></ul></ul></div><h1 id="&#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x7EC4;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;"><a name="&#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x7EC4;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;" class="anchor-navigation-ex-anchor" href="#&#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x7EC4;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x9010;&#x884C;&#x89E3;&#x8BFB;reactive&#x5982;&#x4F55;&#x6784;&#x5EFA;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x7EC4;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;</h1>
<p>&#x5728;petite-vue&#x4E2D;&#x6211;&#x4EEC;&#x901A;&#x8FC7;<code>reactive</code>&#x6784;&#x5EFA;&#x4E0A;&#x4E0B;&#x6587;&#x5BF9;&#x8C61;&#xFF0C;&#x5E76;&#x5C06;&#x6839;&#x636E;&#x72B6;&#x6001;&#x6E32;&#x67D3;UI&#x7684;&#x903B;&#x8F91;&#x4F5C;&#x4E3A;&#x5165;&#x53C2;&#x4F20;&#x9012;&#x7ED9;<code>effect</code>&#xFF0C;&#x7136;&#x540E;&#x795E;&#x5947;&#x7684;&#x4E8B;&#x60C5;&#x53D1;&#x751F;&#x4E86;&#xFF0C;&#x5F53;&#x72B6;&#x6001;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#x5C06;&#x81EA;&#x52A8;&#x89E6;&#x53D1;UI&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x3002;&#x90A3;&#x4E48;&#x5230;&#x5E95;&#x8FD9;&#x662F;&#x600E;&#x4E48;&#x505A;&#x5230;&#x7684;&#x5462;&#xFF1F;
@vue/reactivity&#x529F;&#x80FD;&#x5341;&#x5206;&#x4E30;&#x5BCC;&#xFF0C;&#x800C;petite-vue&#x4EC5;&#x4F7F;&#x7528;&#x5230;<code>reactive</code>&#x548C;<code>effect</code>&#x4E24;&#x4E2A;&#x6700;&#x57FA;&#x672C;&#x7684;API&#xFF0C;&#x4F5C;&#x4E3A;&#x5165;&#x95E8;&#x672C;&#x6587;&#x5C06;&#x4EC5;&#x4EC5;&#x5BF9;&#x8FD9;&#x4E24;&#x4E2A;API&#x8FDB;&#x884C;&#x6E90;&#x7801;&#x89E3;&#x8BFB;&#x3002;</p>
<h2 id="&#x4E00;&#x5207;&#x6E90;&#x4E8E;proxy"><a name="&#x4E00;&#x5207;&#x6E90;&#x4E8E;proxy" class="anchor-navigation-ex-anchor" href="#&#x4E00;&#x5207;&#x6E90;&#x4E8E;proxy"><i class="fa fa-link" aria-hidden="true"></i></a>1.1. &#x4E00;&#x5207;&#x6E90;&#x4E8E;Proxy</h2>
<p>&#x6211;&#x4EEC;&#x77E5;&#x9053;Vue2&#x662F;&#x57FA;&#x4E8E;<code>Object.defineProperty</code>&#x62E6;&#x622A;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x7684;&#x8BFB;&#x5199;&#x64CD;&#x4F5C;&#xFF0C;&#x4ECE;&#x800C;&#x5B9E;&#x73B0;&#x4F9D;&#x8D56;&#x6536;&#x96C6;&#x548C;&#x54CD;&#x5E94;&#x5F0F;UI&#x6E32;&#x67D3;&#x3002;&#x800C;@vue/reactivity&#x4F5C;&#x4E3A;Vue3&#x7684;&#x5B50;&#x9879;&#x76EE;&#xFF0C;&#x91C7;&#x7528;&#x7684;&#x662F;ES6&#x7684;Proxy&#x63A5;&#x53E3;&#x5B9E;&#x73B0;&#x8FD9;&#x4E00;&#x529F;&#x80FD;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> state = {
  count: <span class="hljs-number">1</span>
}

<span class="hljs-keyword">const</span> proxyState = <span class="hljs-keyword">new</span> Proxy(state, {
  <span class="hljs-keyword">get</span>(target: T, property: <span class="hljs-built_in">string</span>, receiver?: T | Proxy): <span class="hljs-built_in">any</span> {
    <span class="hljs-comment">// &#x62E6;&#x622A;&#x8BFB;&#x64CD;&#x4F5C;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;get&apos;</span>)
    <span class="hljs-keyword">return</span> Reflect.get(target, property, receiver)
  },
  <span class="hljs-keyword">set</span>(target: T, property: <span class="hljs-built_in">string</span>, value: <span class="hljs-built_in">any</span>, receiver?: T | Proxy): <span class="hljs-built_in">boolean</span> {
    <span class="hljs-comment">// &#x62E6;&#x622A;&#x5199;&#x64CD;&#x4F5C;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;set&apos;</span>)
    <span class="hljs-keyword">return</span> Reflect.set(target, property, value, receiver)
  },
  deleteProperty(target, prop) {
    <span class="hljs-comment">// &#x62E6;&#x622A;&#x5C5E;&#x6027;&#x5220;&#x9664;&#x64CD;&#x4F5C;</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;delete&apos;</span>)
    <span class="hljs-keyword">delete</span> target[prop]
    <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
  }
})
</code></pre>
<p>&#x76F8;&#x5BF9;<code>Object.defineProperty</code>&#xFF0C;Proxy&#x7684;&#x7279;&#x70B9;&#xFF1A;</p>
<ol>
<li>&#x901A;&#x8FC7;<code>new Proxy</code>&#x6784;&#x5EFA;&#x7684;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x624D;&#x80FD;&#x62E6;&#x622A;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x7684;&#x8BFB;&#x5199;&#x64CD;&#x4F5C;&#xFF0C;&#x800C;&#x88AB;&#x4EE3;&#x7406;&#x7684;&#x5BF9;&#x8C61;&#x5219;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x53D8;&#x5316;&#xFF1B;</li>
<li>&#x53EF;&#x4EE5;&#x76D1;&#x542C;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x7684;&#x53D8;&#x5316;&#x548C;&#x589E;&#x51CF;&#xFF1B;</li>
<li>&#x53EF;&#x4EE5;&#x76D1;&#x542C;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x7684;&#x589E;&#x51CF;&#xFF1B;</li>
<li>Proxy&#x53EF;&#x4EE5;&#x9010;&#x5C42;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#xFF0C;&#x800C;<code>Object.defineProperty</code>&#x5219;&#x9700;&#x8981;&#x4E00;&#x6B21;&#x6027;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x6240;&#x6709;&#x5C42;&#x7EA7;&#x7684;&#x5C5E;&#x6027;&#x3002;</li>
</ol>
<h3 id="&#x54CD;&#x5E94;&#x5F0F;&#x7F16;&#x7A0B;"><a name="&#x54CD;&#x5E94;&#x5F0F;&#x7F16;&#x7A0B;" class="anchor-navigation-ex-anchor" href="#&#x54CD;&#x5E94;&#x5F0F;&#x7F16;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1.1. &#x54CD;&#x5E94;&#x5F0F;&#x7F16;&#x7A0B;</h3>
<pre><code class="lang-ts"><span class="hljs-comment">// &#x5B9A;&#x4E49;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;</span>
<span class="hljs-keyword">const</span> state = reactive({
  num1: <span class="hljs-number">1</span>,
  num2: <span class="hljs-number">2</span>
})

<span class="hljs-comment">// &#x5728;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x4E2D;&#x8BBF;&#x95EE;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#xFF0C;&#x5F53;&#x8FD9;&#x4E9B;&#x5C5E;&#x6027;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x5C06;&#x88AB;&#x81EA;&#x52A8;&#x8C03;&#x7528;</span>
effect(() =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;outer&apos;</span>, state.num1)
  effect(() =&gt; {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;inner&apos;</span>, state.num2)
  })
})
<span class="hljs-comment">// &#x56DE;&#x663E; outer 1</span>
<span class="hljs-comment">// &#x56DE;&#x663E; inner 2</span>

state.num2 += <span class="hljs-number">1</span>
<span class="hljs-comment">// &#x56DE;&#x663E; inner 3</span>

state.num1 += <span class="hljs-number">1</span>
<span class="hljs-comment">// &#x56DE;&#x663E; outer 2</span>
<span class="hljs-comment">// &#x56DE;&#x663E; inner 3</span>

state.num2 += <span class="hljs-number">1</span>
<span class="hljs-comment">// &#x56DE;&#x663E; inner 4</span>
<span class="hljs-comment">// &#x56DE;&#x663E; inner 4</span>
</code></pre>
<p>&#x672C;&#x7BC7;&#x6211;&#x4EEC;&#x5C06;&#x4ECE;reactive&#x5165;&#x624B;&#xFF0C;&#x89E3;&#x8BFB;Vue3&#x5230;&#x5E95;&#x5982;&#x4F55;&#x6784;&#x9020;&#x4E00;&#x4E2A;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x3002;</p>
<h2 id="&#x6DF1;&#x5165;reactive&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;"><a name="&#x6DF1;&#x5165;reactive&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;" class="anchor-navigation-ex-anchor" href="#&#x6DF1;&#x5165;reactive&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2. &#x6DF1;&#x5165;reactive&#x7684;&#x5DE5;&#x4F5C;&#x539F;&#x7406;</h2>
<p>@vue/reactivity&#x7684;&#x6E90;&#x7801;&#x4F4D;&#x4E8E;vue-next&#x9879;&#x76EE;&#x7684;packages/reactivity&#x4E0B;&#xFF0C;&#x800C;<code>reactive</code>&#x51FD;&#x6570;&#x5219;&#x4F4D;&#x4E8E;&#x5176;&#x4E0B;&#x7684;src/reactive.ts&#x6587;&#x4EF6;&#x4E2D;&#x3002;&#x8BE5;&#x6587;&#x4EF6;&#x4E2D;&#x9664;&#x4E86;&#x5305;&#x542B;<code>reactive</code>&#x51FD;&#x6570;&#x5916;&#xFF0C;&#x8FD8;&#x5305;&#x542B;&#x5982;<code>shallowReactive</code>&#x3001;<code>readonly</code>&#x3001;<code>shallowReadonly</code>&#x548C;&#x5176;&#x5B83;&#x5E2E;&#x52A9;&#x51FD;&#x6570;&#x3002;
&#x800C;<code>reactive</code>&#x6838;&#x5FC3;&#x5DE5;&#x4F5C;&#x5219;&#x662F;&#x901A;&#x8FC7;Proxy&#x5C06;&#x4E00;&#x4E2A;&#x666E;&#x901A;&#x7684;JavaScript&#x5BF9;&#x8C61;&#x8F6C;&#x6362;&#x4E3A;&#x76D1;&#x63A7;&#x5BF9;&#x8C61;&#xFF0C;&#x62E6;&#x622A;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x7684;&#x8BFB;&#x5199;&#x5220;&#x64CD;&#x4F5C;&#xFF0C;&#x5E76;&#x6536;&#x96C6;&#x4F9D;&#x8D56;&#x8BE5;&#x5BF9;&#x8C61;&#xFF08;&#x5C5E;&#x6027;&#xFF09;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x3002;&#x5927;&#x81F4;&#x6D41;&#x7A0B;&#x5982;&#x4E0B;&#xFF1A;</p>
<ol>
<li>&#x901A;&#x8FC7;<code>reactive</code>&#x6784;&#x9020;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x90FD;&#x4F1A;&#x5C06;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x548C;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x7684;&#x6620;&#x5C04;&#x5173;&#x7CFB;&#x4FDD;&#x5B58;&#x5728;<code>reactiveMap</code>&#xFF0C;&#x9632;&#x6B62;&#x91CD;&#x590D;&#x751F;&#x6210;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#xFF0C;&#x4F18;&#x5316;&#x6027;&#x80FD;;</li>
<li>&#x5F53;&#x8C03;&#x7528;<code>reactive</code>&#x540E;&#x4F1A;&#x5BF9;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x68C0;&#x67E5;&#xFF0C;&#x82E5;&#x4E0D;&#x662F;&#x53EA;&#x8BFB;&#x5BF9;&#x8C61;&#x3001;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x3001;primitive value&#x548C;<code>reactiveMap</code>&#x4E2D;&#x4E0D;&#x5B58;&#x5728;&#x5219;&#x6839;&#x636E;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#x6784;&#x9020;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;</li>
<li>&#x62E6;&#x622A;&#x8BFB;&#x64CD;&#x4F5C;(<code>get</code>,<code>has</code>&#x548C;<code>ownKeys</code>)&#x65F6;&#x8C03;&#x7528;effect.ts&#x4E2D;&#x7684;track&#x6536;&#x96C6;&#x4F9D;&#x8D56;</li>
<li>&#x62E6;&#x622A;&#x5199;&#x64CD;&#x4F5C;(<code>set</code>, <code>deleteProperty</code>)&#x65F6;&#x8C03;&#x7528;effect.ts&#x4E2D;&#x7684;trigger&#x89E6;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;</li>
</ol>
<p>&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x4E00;&#x8D77;&#x9010;&#x884C;&#x7406;&#x89E3;&#x6E90;&#x7801;&#x5427;&#xFF01;</p>
<h3 id="&#x6E90;&#x7801;&#x89E3;&#x8BFB;&#x2014;&#x2014;reactive&#x5165;&#x53E3;"><a name="&#x6E90;&#x7801;&#x89E3;&#x8BFB;&#x2014;&#x2014;reactive&#x5165;&#x53E3;" class="anchor-navigation-ex-anchor" href="#&#x6E90;&#x7801;&#x89E3;&#x8BFB;&#x2014;&#x2014;reactive&#x5165;&#x53E3;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.1. &#x6E90;&#x7801;&#x89E3;&#x8BFB;&#x2014;&#x2014;reactive&#x5165;&#x53E3;</h3>
<pre><code class="lang-ts"><span class="hljs-comment">// Vue3&#x5185;&#x90E8;&#x5B9A;&#x4E49;&#x7684;&#x5BF9;&#x8C61;&#x7279;&#x6027;&#x6807;&#x8BC6;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> <span class="hljs-keyword">enum</span> ReactiveFlags {
  SKIP = <span class="hljs-string">&apos;__v_skip&apos;</span>, <span class="hljs-comment">// &#x6807;&#x8BC6;&#x8BE5;&#x5BF9;&#x8C61;&#x4E0D;&#x88AB;&#x4EE3;&#x7406;</span>
  IS_REACTIVE = <span class="hljs-string">&apos;__v_isReactive&apos;</span>, <span class="hljs-comment">// &#x6807;&#x8BC6;&#x8BE5;&#x5BF9;&#x8C61;&#x662F;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;</span>
  IS_READONLY = <span class="hljs-string">&apos;__v_isReadonly&apos;</span>, <span class="hljs-comment">// &#x6807;&#x8BC6;&#x8BE5;&#x5BF9;&#x8C61;&#x4E3A;&#x53EA;&#x8BFB;&#x5BF9;&#x8C61;</span>
  RAW = <span class="hljs-string">&apos;__v_raw&apos;</span> <span class="hljs-comment">// &#x6307;&#x5411;&#x88AB;&#x4EE3;&#x7406;&#x7684;JavaScript&#x5BF9;&#x8C61;</span>
}

<span class="hljs-comment">// &#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x7684;&#x63A5;&#x53E3;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">interface</span> Target {
  [ReactiveFlags.SKIP]?: <span class="hljs-built_in">boolean</span>
  [ReactiveFlags.IS_REACTIVE]?: <span class="hljs-built_in">boolean</span>
  [ReactiveFlags.IS_READONLY]?: <span class="hljs-built_in">boolean</span>
  [ReactiveFlags.RAW]?: <span class="hljs-built_in">any</span> <span class="hljs-comment">// &#x7528;&#x4E8E;&#x6307;&#x5411;&#x88AB;&#x4EE3;&#x7406;&#x7684;JavaScript&#x5BF9;&#x8C61;</span>
}

<span class="hljs-comment">// &#x7528;&#x4E8E;&#x7F13;&#x5B58;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x548C;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x7684;&#x5173;&#x7CFB;&#xFF0C;&#x9632;&#x6B62;&#x91CD;&#x590D;&#x4EE3;&#x7406;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> reactiveMap = <span class="hljs-keyword">new</span> WeakMap&lt;Target, <span class="hljs-built_in">any</span>&gt;()

<span class="hljs-comment">// &#x5C06;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x7684;&#x5904;&#x7406;&#x65B9;&#x5F0F;&#x5206;&#x4E3A;&#x4E0D;&#x4EE3;&#x7406;(INVALID)&#x3001;&#x666E;&#x901A;&#x5BF9;&#x8C61;&#x548C;&#x6570;&#x7EC4;(COMMON)&#x548C;Map&#x3001;Set(COLLECTION)</span>
<span class="hljs-keyword">const</span> <span class="hljs-keyword">enum</span> TargetType {
  INVALID = <span class="hljs-number">0</span>,
  COMMON = <span class="hljs-number">1</span>,
  COLLECTION = <span class="hljs-number">2</span>,
}

<span class="hljs-comment">// &#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;&#x53EA;&#x80FD;&#x5904;&#x7406;&#x666E;&#x901A;&#x5BF9;&#x8C61;&#x3001;&#x6570;&#x7EC4;&#x3001;Map/WeakMap&#x548C;Set/WeakSet</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">targetTypeMap</span>(<span class="hljs-params">rawType: <span class="hljs-built_in">string</span></span>) </span>{
  <span class="hljs-keyword">switch</span>(rawType) {
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;Object&apos;</span>:
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;Array&apos;</span>:
      <span class="hljs-keyword">return</span> TargetType.COMMON
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;Map&apos;</span>:
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;Set&apos;</span>:
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;WeakMap&apos;</span>:
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;WeakSet&apos;</span>:
      <span class="hljs-keyword">return</span> TargetType.COLLECTION
    <span class="hljs-keyword">default</span>:
      <span class="hljs-keyword">return</span> TargetType.INVALID
  }
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getTargetType</span>(<span class="hljs-params">value: Target</span>) </span>{
  <span class="hljs-comment">// &#x82E5;&#x5BF9;&#x8C61;&#x6807;&#x8BB0;&#x4E3A;&#x8DF3;&#x8FC7;&#xFF0C;&#x6216;&#x4E0D;&#x53EF;&#x6269;&#x5C55;&#x5219;&#x4E0D;&#x4EE3;&#x7406;&#x8BE5;&#x5BF9;&#x8C61;</span>
  <span class="hljs-keyword">return</span> value[ReactiveFlags.SKIP] || !<span class="hljs-built_in">Object</span>.isExtensible(value)
    ? TargetType.INVALID
    <span class="hljs-comment">// &#x6839;&#x636E;&#x7C7B;&#x578B;&#x51B3;&#x5B9A;&#x5904;&#x7406;&#x65B9;&#x5F0F;</span>
    : targetTypeMap(toRawType(value))
}

<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">reative</span>(<span class="hljs-params">target: object</span>) </span>{
  <span class="hljs-comment">// &#x4E0D;&#x62E6;&#x622A;&#x53EA;&#x8BFB;&#x5BF9;&#x8C61;&#x7684;&#x8BFB;&#x5199;&#x5220;&#x64CD;&#x4F5C;</span>
  <span class="hljs-keyword">if</span> (target &amp;&amp; (target as Target)[ReactiveFlags.IS_READONLY]) {
    <span class="hljs-keyword">return</span> target
  }
  <span class="hljs-keyword">return</span> createReactiveObject(
    target,
    <span class="hljs-literal">false</span>,
    mutableHandlers,
    mutableCollectionHandlers,
    reactiveMap
  )
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createReactiveObject</span> (<span class="hljs-params">
  target: Target,
  isReadonly: <span class="hljs-built_in">boolean</span>,
  baseHandlers: ProxyHandler&lt;<span class="hljs-built_in">any</span>&gt;,
  collectionHandlers: ProxyHandler&lt;<span class="hljs-built_in">any</span>&gt;,
  proxyMap: WeakMap&lt;Target, <span class="hljs-built_in">any</span>&gt;
</span>) </span>{
  <span class="hljs-comment">// reactive&#x51FD;&#x6570;&#x5165;&#x53C2;&#x5FC5;&#x987B;&#x662F;JavaScript&#x5BF9;&#x8C61;&#x6216;&#x6570;&#x7EC4;&#xFF0C;&#x82E5;&#x662F;primitive value&#x5219;&#x4F1A;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;primitive value</span>
  <span class="hljs-keyword">if</span> (!isObject(target)) {
    <span class="hljs-keyword">return</span> target
  }
  <span class="hljs-comment">/**
   * 1. &#x4EC5;&#x80FD;&#x5BF9;&#x975E;&#x54CD;&#x5E94;&#x5F0F;&#x548C;&#x975E;&#x53EA;&#x8BFB;&#x5BF9;&#x8C61;&#x6784;&#x9020;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;
   * 2. &#x53EF;&#x4EE5;&#x5BF9;&#x975E;&#x53EA;&#x8BFB;&#x5BF9;&#x8C61;&#x6784;&#x9020;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;
   */</span>
  <span class="hljs-keyword">if</span> (
    target[ReactiveFlags.RAW] &amp;&amp;
    !(isReadonly &amp;&amp; target[ReactiveFlags.IS_REACTIVE])
  ) {
    <span class="hljs-keyword">return</span> target
  }
  <span class="hljs-comment">// &#x82E5;&#x5BF9;&#x8C61;&#x5DF2;&#x88AB;&#x4EE3;&#x7406;&#x8FC7;&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x5BF9;&#x5E94;&#x7684;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;</span>
  <span class="hljs-keyword">const</span> existingProxy = proxyMap.get(target)
  <span class="hljs-keyword">if</span> (existingProxy) {
    <span class="hljs-keyword">return</span> existingProxy
  }
  <span class="hljs-comment">// &#x6839;&#x636E;&#x88AB;&#x4EE3;&#x7406;&#x7684;&#x5BF9;&#x8C61;&#x7C7B;&#x578B;&#x51B3;&#x5B9A;&#x91C7;&#x7528;&#x54EA;&#x79CD;&#x4EE3;&#x7406;&#x65B9;&#x5F0F;</span>
  <span class="hljs-keyword">const</span> targetType = getTargetType(target)
  <span class="hljs-keyword">if</span> (targetType === TargetType.INVALID) {
    <span class="hljs-keyword">return</span> target
  }
  <span class="hljs-keyword">const</span> proxy = <span class="hljs-keyword">new</span> Proxy (
    target,
    targetType == TargetType.COLLECTION ? collectionHandlers : baseHandlers
  )
  proxyMap.set(target, proxy)
  <span class="hljs-keyword">return</span> proxy
}
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x5230;<code>reactive</code>&#x65B9;&#x6CD5;&#x4E2D;&#x4F1A;&#x5BF9;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5404;&#x79CD;&#x68C0;&#x67E5;&#xFF0C;&#x4ECE;&#x800C;&#x51CF;&#x5C11;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x64CD;&#x4F5C;&#x63D0;&#x9AD8;&#x6027;&#x80FD;&#x3002;&#x6700;&#x540E;&#x82E5;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x578B;&#x4E3A;<code>Object</code>&#x6216;<code>Array</code>&#x5219;&#x91C7;&#x7528;<code>baseHandlers</code>&#x751F;&#x6210;&#x4EE3;&#x7406;&#xFF0C;&#x5426;&#x5219;&#x4F7F;&#x7528;<code>collectionHandlers</code>&#x751F;&#x6210;&#x4EE3;&#x7406;&#x3002;</p>
<h3 id="&#x6E90;&#x7801;&#x89E3;&#x8BFB;-&#x4EE3;&#x7406;object&#x548C;array&#x7684;basehandlers"><a name="&#x6E90;&#x7801;&#x89E3;&#x8BFB;-&#x4EE3;&#x7406;object&#x548C;array&#x7684;basehandlers" class="anchor-navigation-ex-anchor" href="#&#x6E90;&#x7801;&#x89E3;&#x8BFB;-&#x4EE3;&#x7406;object&#x548C;array&#x7684;basehandlers"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.2. &#x6E90;&#x7801;&#x89E3;&#x8BFB;-&#x4EE3;&#x7406;Object&#x548C;Array&#x7684;baseHandlers</h3>
<pre><code class="lang-ts"><span class="hljs-comment">//&#x6587;&#x4EF6; ./baseHandlers.ts</span>

<span class="hljs-comment">// /*#__PURE__*/&#x7528;&#x4E8E;&#x544A;&#x8BC9;webpack&#x7B49;bundler&#x5DE5;&#x5177;&#x540E;&#x9762;&#x7D27;&#x8DDF;&#x7684;&#x51FD;&#x6570;&#x662F;&#x7EAF;&#x51FD;&#x6570;&#xFF0C;&#x82E5;&#x6CA1;&#x88AB;&#x8C03;&#x7528;&#x8FC7;&#x5219;&#x53EF;&#x4EE5;&#x91C7;&#x7528;tree-shaking&#x79FB;&#x9664;&#x6389;&#x8BE5;&#x51FD;&#x6570;</span>
<span class="hljs-keyword">const</span> <span class="hljs-keyword">get</span> = <span class="hljs-comment">/*#__PURE__*/</span> createGetter()

<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> mutableHandlers: ProxyHandler&lt;object&gt; = {
  <span class="hljs-keyword">get</span>,
  <span class="hljs-keyword">set</span>,
  deleteProperty,
  has,
  ownKeys,
}
</code></pre>
<p>&#x6211;&#x4EEC;&#x9996;&#x5148;&#x770B;&#x770B;&#x662F;&#x5982;&#x4F55;&#x62E6;&#x622A;&#x8BFB;&#x64CD;&#x4F5C;&#x5427;</p>
<h4 id="&#x62E6;&#x622A;&#x8BFB;&#x64CD;&#x4F5C;"><a name="&#x62E6;&#x622A;&#x8BFB;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x62E6;&#x622A;&#x8BFB;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x62E6;&#x622A;&#x8BFB;&#x64CD;&#x4F5C;</h4>
<p>&#x62E6;&#x622A;&#x8BFB;&#x64CD;&#x4F5C;&#x6838;&#x5FC3;&#x662F;&#x6536;&#x96C6;&#x4F9D;&#x8D56;&#x6240;&#x8BFB;&#x5C5E;&#x6027;&#x7684;&#x8F85;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x5177;&#x4F53;&#x6D41;&#x7A0B;&#x903B;&#x8F91;&#x662F;</p>
<ol>
<li>&#x5BF9;&#x4E8E;Vue3&#x5185;&#x90E8;&#x5C5E;&#x6027;&#x7684;&#x8BFB;&#x64CD;&#x4F5C;&#xFF0C;&#x5373;&#x8FD4;&#x56DE;&#x5BF9;&#x5E94;&#x7684;&#x503C;&#x800C;&#x4E0D;&#x7528;&#x6536;&#x96C6;&#x4F9D;&#x8D56;</li>
<li>&#x5BF9;&#x4E8E;&#x6570;&#x7EC4;&#x5185;&#x7F6E;&#x65B9;&#x6CD5;&#x7684;&#x8BFB;&#x64CD;&#x4F5C;&#xFF0C;&#x9700;&#x8981;&#x6539;&#x5199;&#x8FD9;&#x4E9B;&#x5185;&#x7F6E;&#x65B9;&#x6CD5;&#x7528;&#x4E8E;&#x5728;&#x8C03;&#x7528;&#x8BE5;&#x65B9;&#x6CD5;&#x524D;&#x5BF9;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x8FDB;&#x884C;&#x4F9D;&#x8D56;&#x6536;&#x96C6;&#xFF0C;&#x6216;&#x89E3;&#x51B3;&#x4E00;&#x4E9B;&#x8FB9;&#x754C;&#x95EE;&#x9898;</li>
<li>&#x5BF9;&#x4E8E;&#x5185;&#x7F6E;Symbol&#x5C5E;&#x6027;&#x548C;&#x5176;&#x5B83;Vue3&#x5185;&#x90E8;&#x5C5E;&#x6027;&#x7684;&#x8BFB;&#x64CD;&#x4F5C;&#xFF0C;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x539F;&#x59CB;&#x503C;&#x4E14;&#x4E0D;&#x7528;&#x6536;&#x96C6;&#x4F9D;&#x8D56;</li>
<li>&#x5BF9;&#x4E8E;&#x975E;&#x53EA;&#x8BFB;&#x5BF9;&#x8C61;&#x7684;&#x9664;&#x4E0A;&#x8FF0;&#x5916;&#x7684;&#x5176;&#x4F59;&#x5C5E;&#x6027;&#x7684;&#x8BFB;&#x64CD;&#x4F5C;&#xFF0C;&#x6267;&#x884C;&#x4F9D;&#x8D56;&#x6536;&#x96C6;(<em>&#x6838;&#x5FC3;&#x903B;&#x8F91;</em>)</li>
<li>&#x82E5;&#x6D45;&#x5C42;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x5219;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x5C5E;&#x6027;&#x503C;&#xFF0C;&#x5426;&#x5219;&#x82E5;&#x5C5E;&#x6027;&#x503C;&#x4E3A;&#x5BF9;&#x8C61;&#xFF0C;&#x5219;&#x5C06;&#x5176;&#x6784;&#x9020;&#x4E3A;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;(<code>reactive</code>)&#x6216;&#x53EA;&#x8BFB;&#x5BF9;&#x8C61;(<code>readonly</code>)</li>
</ol>
<pre><code class="lang-ts"><span class="hljs-comment">//&#x6587;&#x4EF6; ./baseHandlers.ts</span>

<span class="hljs-comment">/**
 * isNonTrackableKeys = {&apos;__proto__&apos;: true, &apos;__v_isRef&apos;: true, &apos;__isVue&apos;: true}
 */</span>
<span class="hljs-keyword">const</span> isNonTrackableKeys = <span class="hljs-comment">/*#__PURE__*/</span> makeMap(<span class="hljs-string">`__proto__,__v_isRef,__isVue`</span>)

<span class="hljs-comment">// &#x5185;&#x7F6E;&#x7684;Symbol&#x5B9E;&#x4F8B;&#x5305;&#x542B;&#xFF1A;hasInstance, isConcatSpreadable, iterator, asyncIterator, match, matchAll, replace, search, split, toPrimitive, toStringTag, species, unscopables</span>
<span class="hljs-keyword">const</span> builtInSymbols = <span class="hljs-keyword">new</span> Set(
  <span class="hljs-built_in">Object</span>.getOwnPropertyNames(Symbol)
    .map(key =&gt; (Symbol as <span class="hljs-built_in">any</span>)[key])
    .filter(isSymbol)
)

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createGetter</span>(<span class="hljs-params">isReadonly = <span class="hljs-literal">false</span>, shallow = <span class="hljs-literal">false</span></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">get</span>(<span class="hljs-params">target: Target, key: <span class="hljs-built_in">string</span> | symbol, receiver: object</span>) </span>{
    <span class="hljs-comment">// &#x5904;&#x7406;Vue3&#x5185;&#x90E8;&#x5C5E;&#x6027;&#x540D;(`__v_isReactive`, `__v_isReadonly`, `__v_raw`)&#x7684;&#x503C;</span>
    <span class="hljs-keyword">if</span> (key === ReactiveFlags.IS_REACTIVE) {
      <span class="hljs-keyword">return</span> !isReadonly
    }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (key === ReactiveFlags.IS_READONLY) {
      <span class="hljs-keyword">return</span> isReadonly
    }
    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (
      key === ReactiveFlags.RAW &amp;&amp;
      receiver === reactiveMap.get(target)
    ) {
      <span class="hljs-keyword">return</span> target
    }

    <span class="hljs-comment">// &#x5982;&#x679C;key&#x662F;includes,indexOf,lastIndexOf,push,pop,shift,unshift,splice&#x65F6;&#x5219;&#x8FD4;&#x56DE;&#x80FD;&#x8DDF;&#x8E2A;&#x4F9D;&#x8D56;&#x53D8;&#x5316;&#x7684;&#x7248;&#x672C;</span>
    <span class="hljs-keyword">const</span> targetIsArray = isArray(target)
    <span class="hljs-keyword">if</span> (!isReadonly &amp;&amp; targetIsArray &amp;&amp; hasOwn(arrayInstrumentations, key)) {
      <span class="hljs-keyword">return</span> Reflect.get(arrayInstrumentations, key, receiver)
    }

    <span class="hljs-keyword">const</span> res = Reflect.get(target, key, receiver)

    <span class="hljs-comment">// &#x4E0D;&#x62E6;&#x622A;&#x5185;&#x7F6E;Symbol&#x5C5E;&#x6027;&#x548C;__proto__,__v_isRef&#x548C;__isVue&#x5C5E;&#x6027;</span>
    <span class="hljs-keyword">if</span> (isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
      <span class="hljs-keyword">return</span> res
    }

    <span class="hljs-comment">// &#x6536;&#x96C6;&#x4F9D;&#x8D56;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</span>
    <span class="hljs-keyword">if</span> (!isReadonly) {
      track(target, TrackOpTypes.GET, key)
    }

    <span class="hljs-comment">// &#x5982;&#x679C;&#x662F;&#x6784;&#x5EFA;ShallowReactive&#x5219;&#x4E0D;&#x4F1A;&#x57FA;&#x4E8E;&#x5C5E;&#x6027;&#x503C;&#x6784;&#x9020;&#x54CD;&#x5E94;&#x5F0F;&#x5F0F;&#x5BF9;&#x8C61;</span>
    <span class="hljs-keyword">if</span> (shallow) {
      <span class="hljs-keyword">return</span> res
    }

    <span class="hljs-comment">/* &#x5BF9;&#x4E8E;&#x5C5E;&#x6027;&#x503C;&#x4E3A;@vue/reactivity&#x7684;Ref&#x5B9E;&#x4F8B;&#x65F6;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#x6267;&#x884C;[1,2,3][0]&#x7684;&#x64CD;&#x4F5C;&#x5219;&#x8FD4;&#x56DE;Ref&#x5B9E;&#x4F8B;&#x5305;&#x542B;&#x7684;primitive value&#xFF0C;&#x5426;&#x5219;&#x8FD4;&#x56DE;Ref&#x5B9E;&#x4F8B;
     * &#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x5728;effect updator&#x4E2D;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5982;&#x4E0B;&#x65B9;&#x5F0F;&#x76F4;&#x63A5;&#x83B7;&#x53D6;Ref&#x5B9E;&#x4F8B;&#x5C5E;&#x6027;&#x7684;primitive value
     * const age = ref(0), state = reactive({ age })
     * console.log(age.value) // &#x56DE;&#x663E;0
     * effect(() =&gt; { console.log(state.age) }) // &#x56DE;&#x663E;0
     */</span>
    <span class="hljs-keyword">if</span> (isRef(res)) {
      <span class="hljs-keyword">const</span> shouldUnwrap = !targetIsArray || !isIntegerKey(key)
      <span class="hljs-keyword">return</span> shouldUnwrap ? res.value : res
    }

    <span class="hljs-comment">// &#x82E5;&#x5C5E;&#x6027;&#x503C;&#x4E0D;&#x662F;primitive value&#x6216;BOM&#xFF0C;&#x5219;&#x57FA;&#x4E8E;&#x5C5E;&#x6027;&#x503C;&#x6784;&#x9020;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;</span>
    <span class="hljs-keyword">if</span> (isObject(res)) {
      <span class="hljs-keyword">return</span> isReadonly ? readonly(res) :  reactive(res)
    }
  }
}
</code></pre>
<p>&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5F53;&#x8BFB;&#x53D6;&#x5C5E;&#x6027;&#x65F6;&#x624D;&#x6839;&#x636E;&#x5C5E;&#x6027;&#x503C;&#x7C7B;&#x578B;&#x6765;&#x4E3A;&#x5C5E;&#x6027;&#x503C;&#x6784;&#x9020;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x5F53;&#x6211;&#x4EEC;&#x8C03;&#x7528;<code>reactive</code>&#x65F6;&#x5C31;&#x4E00;&#x80A1;&#x8111;&#x7684;&#x904D;&#x5386;&#x5BF9;&#x8C61;&#x6240;&#x6709;&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4E3A;&#x5404;&#x4E2A;&#x5C5E;&#x6027;&#x6784;&#x5EFA;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x53E6;&#x5916;&#xFF0C;&#x9488;&#x5BF9;<code>includes</code>&#x7B49;&#x6570;&#x7EC4;&#x64CD;&#x4F5C;&#x4F1A;&#x8FD4;&#x56DE;&#x5BF9;&#x5E94;&#x7684;&#x80FD;&#x8DDF;&#x8E2A;&#x4F9D;&#x8D56;&#x53D8;&#x5316;&#x7684;&#x7248;&#x672C;&#xFF0C;&#x5230;&#x5E95;&#x4EC0;&#x4E48;&#x662F;&#x80FD;&#x8DDF;&#x8E2A;&#x4F9D;&#x8D56;&#x53D8;&#x5316;&#x7684;&#x7248;&#x672C;&#x5462;&#xFF1F;</p>
<pre><code class="lang-ts"><span class="hljs-comment">// &#x6587;&#x4EF6; ./baseHandlers.ts</span>

<span class="hljs-keyword">const</span> arrayInstrumentations = <span class="hljs-comment">/*#__PURE__*/</span> createArrayInstrumentations()

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createArrayInstrumentations</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> instrumentations: Record&lt;<span class="hljs-built_in">string</span>, <span class="hljs-built_in">Function</span>&gt; = {}
  ;([<span class="hljs-string">&apos;includes&apos;</span>, <span class="hljs-string">&apos;indexOf&apos;</span>, <span class="hljs-string">&apos;lastIndexOf&apos;</span>] as <span class="hljs-keyword">const</span>).forEach(key =&gt; {
    instrumentations[key] = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: unknown[], ...args: unknown[]</span>) </span>{
      <span class="hljs-keyword">const</span> arr = toRaw(<span class="hljs-keyword">this</span>) as <span class="hljs-built_in">any</span>
      <span class="hljs-comment">/**
       * &#x83B7;&#x53D6;&#x539F;&#x59CB;&#x6570;&#x7EC4;&#x5BF9;&#x8C61;&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;&#x6570;&#x7EC4;&#x539F;&#x751F;&#x7684;include,indexOf&#x6216;lastIndexOf&#x65B9;&#x6CD5;&#x83B7;&#x53D6;&#x8FD4;&#x56DE;&#x503C;&#x3002;
       * &#x4F46;&#x7531;&#x4E8E;&#x539F;&#x751F;&#x65B9;&#x6CD5;&#x7684;&#x6267;&#x884C;&#x65E0;&#x6CD5;&#x8DDF;&#x8E2A;&#x88AB;&#x904D;&#x5386;&#x7684;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#xFF0C;&#x56E0;&#x6B64;&#x8FD9;&#x91CC;&#x4F1A;&#x63D0;&#x524D;&#x904D;&#x5386;&#x6570;&#x7EC4;&#x4E2D;&#x7684;&#x6240;&#x6709;&#x5143;&#x7D20;&#xFF0C;&#x5B9E;&#x73B0;&#x6570;&#x7EC4;&#x5143;&#x7D20;&#x53D8;&#x5316;&#x7684;&#x8DDF;&#x8E2A;&#x5904;&#x7406;&#x3002;
       * 
       * &#x8FD9;&#x91CC;&#x6709;&#x4E00;&#x4E2A;&#x5F85;&#x4F18;&#x5316;&#x70B9;&#xFF0C;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x5982;&#x4E0B;&#x793A;&#x4F8B;&#xFF1A;
       * const arr = reactive([2,1,3])
       * effect(() =&gt; {
       *   console.log(arr.indexOf(2))
       * })
       * setTimeout(() =&gt;{
       *   arr[1] = 4
       * }, 5000)
       * &#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x4FEE;&#x6539;arr[1]&#x7684;&#x503C;&#x5C06;&#x4F1A;&#x89E6;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x7684;&#x89E6;&#x53D1;&#xFF0C;&#x4F46;arr[1]&#x503C;&#x7684;&#x53D8;&#x5316;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x56E0;&#x6B64;&#x53EF;&#x4EE5;&#x5224;&#x5B9A;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x65E0;&#x6548;&#x89E6;&#x53D1;&#x3002;
       */</span>
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>, l = <span class="hljs-keyword">this</span>.length; i &lt; l; i++) {
        track(arr, TrackOpTypes.GET, i + <span class="hljs-string">&apos;&apos;</span>)
      }

      <span class="hljs-comment">// &#x8C03;&#x7528;&#x6570;&#x7EC4;&#x539F;&#x751F;&#x7684;includes,indexOf&#x548C;lastIndexOf&#x65B9;&#x6CD5;</span>
      <span class="hljs-keyword">const</span> res = arr[key](...args)
      <span class="hljs-keyword">if</span> (res === <span class="hljs-number">-1</span> <span class="hljs-comment">// indexOf&#x548C;lastIndexOf</span>
          || res === <span class="hljs-literal">false</span> <span class="hljs-comment">// includes</span>
      ) {
        <span class="hljs-comment">// &#x7531;&#x4E8E;&#x5165;&#x53C2;&#x6709;&#x53EF;&#x80FD;&#x662F;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#xFF0C;&#x56E0;&#x6B64;&#x5F53;&#x5339;&#x914D;&#x5931;&#x8D25;&#xFF0C;&#x5219;&#x5C06;&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x5165;&#x53C2;&#x7684;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x91CD;&#x65B0;&#x5339;&#x914D;</span>
        <span class="hljs-keyword">return</span> arr[key](...args.map(toRaw))
      } <span class="hljs-keyword">else</span> {
        <span class="hljs-keyword">return</span> res
      }
    }
  })
  <span class="hljs-comment">// &#x4E0B;&#x9762;&#x7684;&#x64CD;&#x4F5C;&#x4F1A;&#x4FEE;&#x6539;&#x6570;&#x7EC4;&#x7684;&#x957F;&#x5EA6;&#xFF0C;&#x8FD9;&#x91CC;&#x907F;&#x514D;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x957F;&#x5EA6;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;</span>
  ;([<span class="hljs-string">&apos;push&apos;</span>, <span class="hljs-string">&apos;pop&apos;</span>, <span class="hljs-string">&apos;shift&apos;</span>, <span class="hljs-string">&apos;unshift&apos;</span>, <span class="hljs-string">&apos;splice&apos;</span>] as <span class="hljs-keyword">const</span>).forEach(key =&gt; {
    instrumentations[key] = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: unknown[], ...args: unknown[]</span>) </span>{
      pauseTracking()
      <span class="hljs-keyword">const</span> res = (toRaw(<span class="hljs-keyword">this</span>) as <span class="hljs-built_in">any</span>)[key].apply(<span class="hljs-keyword">this</span>, args)
      resetTracking()
      <span class="hljs-keyword">return</span> res
    }
  })

  <span class="hljs-keyword">return</span> instrumentations
}

<span class="hljs-comment">// &#x6587;&#x4EF6; ./reactive.ts</span>
<span class="hljs-keyword">export</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toRaw</span>&lt;<span class="hljs-title">T</span>&gt;(<span class="hljs-params">observed: T</span>): <span class="hljs-title">T</span> </span>{
  <span class="hljs-keyword">const</span> raw = observed &amp;&amp; (observed as Target)[ReactiveFlags.RAW]
  <span class="hljs-keyword">return</span> raw ? toRaw(raw) : observed
}
</code></pre>
<p>TypeScript&#x5C0F;&#x8BFE;&#x5802;1&#xFF1A;<code>[&apos;includes&apos;, &apos;indexOf&apos;, &apos;lastIndexOf&apos;] as const</code>&#x5728;TypeScript&#x4E2D;&#x7528;&#x4E8E;&#x6807;&#x8BC6;&#x5BF9;&#x8C61;&#x6216;&#x6570;&#x7EC4;&#x4E3A;&#x4E0D;&#x53EF;&#x4FEE;&#x6539;&#x5BF9;&#x8C61;&#x3002;&#x5373;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> a = [<span class="hljs-string">&apos;includes&apos;</span>, <span class="hljs-string">&apos;indexOf&apos;</span>, <span class="hljs-string">&apos;lastIndexOf&apos;</span>] as <span class="hljs-keyword">const</span>
a[<span class="hljs-number">0</span>] = <span class="hljs-string">&apos;hi&apos;</span> <span class="hljs-comment">// &#x7F16;&#x8BD1;&#x65F6;&#x62A5;&#x9519;</span>

<span class="hljs-keyword">const</span> b = [<span class="hljs-string">&apos;includes&apos;</span>, <span class="hljs-string">&apos;indexOf&apos;</span>, <span class="hljs-string">&apos;lastIndexOf&apos;</span>]
b[<span class="hljs-number">0</span>] = <span class="hljs-string">&apos;hi&apos;</span> <span class="hljs-comment">// &#x4FEE;&#x6539;&#x6210;&#x529F;</span>
<span class="hljs-built_in">console</span>.log(b[<span class="hljs-number">0</span>]) <span class="hljs-comment">// &#x56DE;&#x663E; hi</span>
</code></pre>
<p>TypeScript&#x5C0F;&#x8BFE;&#x5802;2&#xFF1A;<code>instrumentations[key] = function(this: unknown[], ...args: unknown[]) {...}</code>&#x91C7;&#x7528;&#x7684;&#x662F;TypeScript&#x7684;<code>this</code>&#x53C2;&#x6570;&#xFF0C;&#x7528;&#x4E8E;&#x9650;&#x5236;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x65F6;&#x7684;<code>this</code>&#x7C7B;&#x578B;&#x3002;
&#x8F6C;&#x6362;&#x4E3A;JavaScript&#x5C31;&#x662F;</p>
<pre><code class="lang-js">instrumentations[key] = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">...args</span>)</span>{
  pauseTracking()
  <span class="hljs-keyword">const</span> res = (toRaw(<span class="hljs-keyword">this</span>) <span class="hljs-keyword">as</span> any)[key].apply(<span class="hljs-keyword">this</span>, args)
  resetTracking()
  <span class="hljs-keyword">return</span> res
}
</code></pre>
<h4 id="&#x62E6;&#x622A;&#x5199;&#x64CD;&#x4F5C;"><a name="&#x62E6;&#x622A;&#x5199;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x62E6;&#x622A;&#x5199;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x62E6;&#x622A;&#x5199;&#x64CD;&#x4F5C;</h4>
<p>&#x65E2;&#x7136;&#x62E6;&#x622A;&#x8BFB;&#x64CD;&#x4F5C;&#x662F;&#x4E3A;&#x4E86;&#x6536;&#x96C6;&#x4F9D;&#x8D56;&#xFF0C;&#x90A3;&#x4E48;&#x62E6;&#x622A;&#x5199;&#x64CD;&#x4F5C;&#x81EA;&#x7136;&#x5C31;&#x662F;&#x7528;&#x4E8E;&#x89E6;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x4E86;&#x3002;&#x6D41;&#x7A0B;&#x903B;&#x8F91;&#x5982;&#x4E0B;&#xFF1A;</p>
<ol>
<li>&#x82E5;&#x5C5E;&#x6027;&#x503C;&#x4E3A;Ref&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x65B0;&#x503C;&#x53D6;&#x539F;&#x59CB;&#x503C;&#x540E;&#x4E0D;&#x662F;Ref&#x5BF9;&#x8C61;&#xFF0C;&#x5219;&#x66F4;&#x65B0;Ref&#x5BF9;&#x8C61;&#x7684;value&#xFF0C;&#x7531;Ref&#x5185;&#x90E8;&#x89E6;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</li>
<li>&#x5224;&#x65AD;&#x662F;&#x5426;&#x4E3A;&#x65B0;&#x589E;&#x5C5E;&#x6027;&#xFF0C;&#x8FD8;&#x662F;&#x66F4;&#x65B0;&#x5C5E;&#x6027;&#x503C;&#xFF0C;&#x5E76;&#x89E6;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;</li>
</ol>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> <span class="hljs-keyword">set</span> = <span class="hljs-comment">/*#__PURE__*/</span> createSetter()

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createSetter</span>(<span class="hljs-params">shallow = <span class="hljs-literal">false</span></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">set</span>(<span class="hljs-params">
    target: <span class="hljs-built_in">Object</span>,
    key: <span class="hljs-built_in">string</span> | symbol,
    value: unknown,
    receiver: object
  </span>): <span class="hljs-title">boolean</span> </span>{
    <span class="hljs-comment">// Proxy&#x7684;set&#x62E6;&#x622A;&#x5668;&#x8FD4;&#x56DE;true&#x8868;&#x793A;&#x8D4B;&#x503C;&#x6210;&#x529F;&#xFF0C;false&#x8868;&#x793A;&#x8D4B;&#x503C;&#x5931;&#x8D25;</span>

    <span class="hljs-keyword">let</span> oldValue = (target as <span class="hljs-built_in">any</span>)[key]
    <span class="hljs-keyword">if</span> (!shallow) {
      <span class="hljs-comment">/* &#x82E5;&#x65E7;&#x5C5E;&#x6027;&#x503C;&#x4E3A;Ref&#xFF0C;&#x800C;&#x65B0;&#x503C;&#x4E0D;&#x662F;Ref&#xFF0C;&#x5219;&#x76F4;&#x63A5;&#x5C06;&#x65B0;&#x503C;&#x8D4B;&#x503C;&#x7ED9;&#x65E7;&#x5C5E;&#x6027;&#x7684;value&#x5C5E;&#x6027;
       * &#x4E00;&#x773C;&#x770B;&#x4E0A;&#x53BB;&#x8C8C;&#x4F3C;&#x6CA1;&#x6709;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x538B;&#x5165;&#x8C03;&#x5EA6;&#x5668;&#xFF0C;&#x4F46;Ref&#x5BF9;&#x8C61;&#x4E5F;&#x662F;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#xFF0C;&#x8D4B;&#x503C;&#x7ED9;&#x5B83;&#x7684;value&#x5C5E;&#x6027;&#xFF0C;&#x4F1A;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x8BE5;Ref&#x5BF9;&#x8C61;&#x7684;&#x8F85;&#x4F50;&#x7528;&#x51FD;&#x6570;&#x538B;&#x5165;&#x8C03;&#x5EA6;&#x5668;
       */</span>  
      value = toRaw(value)
      oldValue = toRaw(oldValue)
      <span class="hljs-keyword">if</span> (!isArray(target) &amp;&amp; isRef(oldValue) &amp;&amp; !isRef(value)) {
        oldValue.value = value
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>
      }
    }

    <span class="hljs-comment">// &#x7528;&#x4E8E;&#x5224;&#x65AD;&#x662F;&#x65B0;&#x589E;&#x5C5E;&#x6027;&#x8FD8;&#x662F;&#x4FEE;&#x6539;&#x5C5E;&#x6027;</span>
    <span class="hljs-keyword">const</span> hadKey = 
      isArray(target) &amp;&amp; isIntegerKey(key)
        ? <span class="hljs-built_in">Number</span>(key) &lt; target.length <span class="hljs-comment">// &#x6570;&#x7EC4;&#x7D22;&#x5F15;&#x7684;&#x5904;&#x7406;</span>
        : hasOwn(target, key) <span class="hljs-comment">// &#x5BF9;&#x8C61;&#x6216;&#x6570;&#x7EC4;&#x975E;&#x7D22;&#x5F15;&#x7684;&#x800C;&#x5904;&#x7406;</span>
    <span class="hljs-comment">// &#x8D4B;&#x503C;&#x540E;&#x518D;&#x5C06;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x538B;&#x5165;&#x8C03;&#x5EA6;&#x5668;</span>
    <span class="hljs-keyword">const</span> result = Reflect.set(target, key, value, receiver)
    <span class="hljs-keyword">if</span> (target === toRaw(receiver)) {
      <span class="hljs-keyword">if</span> (!hadKey) {
        <span class="hljs-comment">// &#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x538B;&#x5165;&#x8C03;&#x5EA6;&#x5668;</span>
        trigger(target, TriggerOpTypes.ADD, key, value)
      }
      <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (hasChange(value, oldValue)) {
        <span class="hljs-comment">// &#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x538B;&#x5165;&#x8C03;&#x5EA6;&#x5668;</span>
        trigger(target, TriggerOpTypes.SET, key, value, oldValue)
      }
    }
    <span class="hljs-keyword">return</span> result
  }
}

<span class="hljs-comment">// &#x6587;&#x4EF6; @vue/shared</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> hasChanged = (value: <span class="hljs-built_in">any</span>, oldValue: <span class="hljs-built_in">any</span>): <span class="hljs-built_in">boolean</span> =&gt; !<span class="hljs-built_in">Object</span>.is(value, oldValue)
</code></pre>
<p>&#x4E3A;&#x4EC0;&#x4E48;&#x4E0D;&#x7528;<code>===</code>&#x800C;&#x8981;&#x4F7F;&#x7528;<code>Object.is</code>&#x6765;&#x6BD4;&#x8F83;&#x4E24;&#x4E2A;&#x503C;&#x662F;&#x5426;&#x76F8;&#x7B49;&#x5462;&#xFF1F;
&#x5BF9;&#x4E8E;<code>-0===0</code>&#x8FD4;&#x56DE;<code>true</code>,<code>NaN === NaN</code>&#x8FD4;&#x56DE;<code>false</code>&#xFF0C;&#x800C;<code>Object.is(-0, 0)</code>&#x8FD4;&#x56DE;<code>false</code>,<code>Object.is(NaN, NaN)</code>&#x8FD4;&#x56DE;<code>true</code>&#x3002;
&#x66F4;&#x591A;&#x4FE1;&#x606F;&#x8BF7;&#x67E5;&#x770B;<a href="https://www.cnblogs.com/fsjohnhuang/p/15531508.html" target="_blank">&#x300A;Source Code Reading for Vue 3: How does <code>hasChanged</code> work? &#x300B;</a></p>
<h3 id="&#x62E6;&#x622A;&#x5220;&#x9664;&#x64CD;&#x4F5C;"><a name="&#x62E6;&#x622A;&#x5220;&#x9664;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x62E6;&#x622A;&#x5220;&#x9664;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.3. &#x62E6;&#x622A;&#x5220;&#x9664;&#x64CD;&#x4F5C;</h3>
<p>&#x5220;&#x9664;&#x64CD;&#x4F5C;&#x4F1A;&#x4FEE;&#x6539;&#x5C5E;&#x6027;&#x81EA;&#x7136;&#x4E5F;&#x4F1A;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x5566;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">deleteProperty</span>(<span class="hljs-params">target: object, key: <span class="hljs-built_in">string</span> | symbol</span>): <span class="hljs-title">boolean</span> </span>{
  <span class="hljs-keyword">const</span> hadKey = hasOwn(target, key)
  <span class="hljs-keyword">const</span> oldValue = (target as <span class="hljs-built_in">any</span>)[key]
  <span class="hljs-keyword">const</span> result = Reflect.deleteProperty(target, key)
  <span class="hljs-keyword">if</span> (result &amp;&amp; hadKey) {
    <span class="hljs-comment">// &#x82E5;&#x5220;&#x9664;&#x6210;&#x529F;&#xFF0C;&#x4E14;&#x5B58;&#x5728;&#x65E7;&#x503C;&#x5219;&#x89E6;&#x53D1;&#x4F9D;&#x8D56;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x4EFB;&#x52A1;&#x538B;&#x5165;&#x8C03;&#x5EA6;&#x5668;</span>
    trigger(target, TriggerOpTypes.DELETE, key, <span class="hljs-literal">undefined</span>, oldValue)
  }
  <span class="hljs-keyword">return</span> result
}
</code></pre>
<h3 id="&#x62E6;&#x622A;&#x68C0;&#x67E5;&#x5B58;&#x5728;&#x4E0E;&#x5426;&#x64CD;&#x4F5C;name-in-state"><a name="&#x62E6;&#x622A;&#x68C0;&#x67E5;&#x5B58;&#x5728;&#x4E0E;&#x5426;&#x64CD;&#x4F5C;name-in-state" class="anchor-navigation-ex-anchor" href="#&#x62E6;&#x622A;&#x68C0;&#x67E5;&#x5B58;&#x5728;&#x4E0E;&#x5426;&#x64CD;&#x4F5C;name-in-state"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.4. &#x62E6;&#x622A;&#x68C0;&#x67E5;&#x5B58;&#x5728;&#x4E0E;&#x5426;&#x64CD;&#x4F5C;(&apos;name&apos; in state)</h3>
<p>&#x68C0;&#x67E5;&#x5B58;&#x5728;&#x4E0E;&#x5426;&#x5C5E;&#x4E8E;&#x8BFB;&#x64CD;&#x4F5C;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x4F9D;&#x8D56;&#x6536;&#x96C6;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">has</span>(<span class="hljs-params">target: object, key: <span class="hljs-built_in">string</span> | symbol</span>): <span class="hljs-title">boolean</span> </span>{
  <span class="hljs-keyword">const</span> result = Reflect.has(target, key)
  <span class="hljs-comment">// Symbol&#x5185;&#x7F6E;&#x5C5E;&#x6027;&#x4E0D;&#x6536;&#x96C6;</span>
  <span class="hljs-keyword">if</span> (!isSymbol(key) || !builtInSymbols.has(key)) {
    track(target, TrackOpTypes.HAS, key)
  }
  <span class="hljs-keyword">return</span> result
}
</code></pre>
<h3 id="&#x62E6;&#x622A;&#x952E;&#x904D;&#x5386;&#x64CD;&#x4F5C;"><a name="&#x62E6;&#x622A;&#x952E;&#x904D;&#x5386;&#x64CD;&#x4F5C;" class="anchor-navigation-ex-anchor" href="#&#x62E6;&#x622A;&#x952E;&#x904D;&#x5386;&#x64CD;&#x4F5C;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2.5. &#x62E6;&#x622A;&#x952E;&#x904D;&#x5386;&#x64CD;&#x4F5C;</h3>
<p>&#x4EE5;&#x4E0B;&#x64CD;&#x4F5C;&#x90FD;&#x4F1A;&#x6267;&#x884C;<code>ownKeys</code>Proxy trap&#x65B9;&#x6CD5;</p>
<ul>
<li><code>Object.getOwnPropertyNames</code></li>
<li><code>Object.getOwnPropertySymbols</code></li>
<li><code>Object.keys</code></li>
<li><code>Object.names</code></li>
<li><code>for..in</code></li>
</ul>
<p>&#x6D41;&#x7A0B;&#x903B;&#x8F91;&#x662F;&#xFF1A;&#x5BF9;&#x4E8E;&#x6570;&#x7EC4;&#x5219;&#x8DDF;&#x8E2A;&#x6570;&#x7EC4;&#x957F;&#x5EA6;&#xFF0C;&#x5426;&#x5219;&#x8DDF;&#x8E2A;&#x7531;effect&#x6A21;&#x5757;&#x63D0;&#x4F9B;&#x7684;<code>ITERATE_KEY</code>&#xFF0C;&#x8FD9;&#x4E2A;&#x662F;&#x4EC0;&#x4E48;&#x4E1C;&#x4E1C;&#x5462;&#xFF1F;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x770B;&#x5C31;&#x77E5;&#x9053;&#x4E86;:)</p>
<pre><code class="lang-ts">function ownKeys(target: object): (string | symbol)[] {
  track(target, TrackOpTypes.ITERATE, isArray(target) ? &apos;length&apos; : ITERATE_KEY)
  return Reflect.ownKeys(target)
}
</code></pre>
<h2 id="proxy&#x4E2D;&#x7684;receiver&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><a name="proxy&#x4E2D;&#x7684;receiver&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;" class="anchor-navigation-ex-anchor" href="#proxy&#x4E2D;&#x7684;receiver&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3. Proxy&#x4E2D;&#x7684;receiver&#x5230;&#x5E95;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</h2>
<p>&#x5728;&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x4E2D;&#x6211;&#x4EEC;&#x53D1;&#x73B0;&#x4F1A;&#x4F7F;&#x7528;&#x5230;Proxy&#x62E6;&#x622A;&#x51FD;&#x6570;&#x5165;&#x53C2;<code>receiver</code>&#xFF0C;&#x5982;&#xFF1A;</p>
<ol>
<li>&#x5728;&#x8BFB;&#x5199;&#x62E6;&#x622A;&#x4E2D;&#x90FD;&#x4F1A;&#x8C03;&#x7528;<code>Reflect.get(xxx, xxx, receiver)</code>&#x3001;<code>Reflect.get(xxx, xxx, receiver)</code>.</li>
<li>&#x5728;&#x5199;&#x5165;&#x62E6;&#x622A;&#x65F6;&#xFF0C;&#x5982;&#x679C;<code>target === toRaw(receiver)</code>&#x6210;&#x7ACB;&#x5219;&#x89E6;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;</li>
<li><p>&#x5728;&#x5F00;&#x7BC7;<a href="https://www.cnblogs.com/fsjohnhuang/p/15964344.html" target="_blank">&#x300A;petite-vue&#x6E90;&#x7801;&#x5256;&#x6790;-&#x4ECE;&#x9759;&#x6001;&#x89C6;&#x56FE;&#x5F00;&#x59CB;&#x300B;</a>&#x4E2D;&#x521B;&#x5EFA;&#x4F5C;&#x7528;&#x57DF;&#x94FE;<code>createScopedContext</code>&#x6709;&#x5982;&#x4E0B;&#x4EE3;&#x7801;</p>
<pre><code class="lang-ts"> <span class="hljs-keyword">const</span> reactiveProxy = reactive(
   <span class="hljs-keyword">new</span> Proxy(mergeScope, {
     <span class="hljs-keyword">set</span>(target, key, val, receiver) {
       <span class="hljs-comment">// &#x82E5;&#x5F53;&#x8BBE;&#x7F6E;&#x7684;&#x5C5E;&#x6027;&#x4E0D;&#x5B58;&#x5728;&#x4E8E;&#x5F53;&#x524D;&#x4F5C;&#x7528;&#x57DF;&#x5219;&#x5C06;&#x503C;&#x8BBE;&#x7F6E;&#x5230;&#x7236;&#x4F5C;&#x7528;&#x57DF;&#x4E0A;&#xFF0C;&#x7531;&#x4E8E;&#x7236;&#x4F5C;&#x7528;&#x57DF;&#x4EE5;&#x540C;&#x6837;&#x65B9;&#x5F0F;&#x521B;&#x5EFA;&#xFF0C;&#x56E0;&#x6B64;&#x9012;&#x5F52;&#x627E;&#x5230;&#x62E5;&#x6709;&#x8BE5;&#x5C5E;&#x6027;&#x7684;&#x7956;&#x5148;&#x4F5C;&#x7528;&#x57DF;&#x5E76;&#x8D4B;&#x503C;</span>
       <span class="hljs-keyword">if</span> (receiver === reactiveProxy &amp;&amp; !target.hasOwnProperty(key)) {
         <span class="hljs-keyword">return</span> Reflect.set(parentScope, key, val)
       }
       <span class="hljs-keyword">return</span> Reflect.set(target, key, val, receiver)
     }
   })
 )
</code></pre>
</li>
</ol>
<p>&#x90A3;&#x4E48;&#x5230;&#x5E95;<code>receiver</code>&#x662F;&#x4EC0;&#x4E48;&#x5462;&#xFF1F;</p>
<ol>
<li><p>&#x5BF9;&#x4E8E;&#x6570;&#x636E;&#x5C5E;&#x6027;(data properties)&#x7684;&#x62E6;&#x622A;&#xFF0C;<code>receiver</code>&#x6307;&#x5411;&#x5F53;&#x524D;&#x6784;&#x5EFA;&#x7684;<code>Proxy</code>&#x5B9E;&#x4F8B;&#x672C;&#x8EAB;</p>
<pre><code class="lang-js"><span class="hljs-comment">// `receiver`&#x6307;&#x5411;&#x5F53;&#x524D;&#x6784;&#x5EFA;&#x7684;`Proxy`&#x5B9E;&#x4F8B;&#x672C;&#x8EAB;</span>
<span class="hljs-keyword">const</span> state = {
  name: <span class="hljs-string">&apos;john&apos;</span>
}
<span class="hljs-keyword">let</span> pState = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Proxy</span>(state, {
  get(target, key, receiver) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;receiver === pState:&quot;</span>, receiver === pState)
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Reflect</span>.get(target, key, receiver)
  }
})

pState.name
<span class="hljs-comment">// &#x56DE;&#x663E; receiver === pState: true</span>
</code></pre>
</li>
<li><p>&#x5BF9;&#x4E8E;&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;(accessor properties)&#x7684;&#x62E6;&#x622A;&#xFF0C;<code>receiver</code>&#x6307;&#x5411;<code>this</code>&#x6216;&#x8005;&#x7EE7;&#x627F;<code>Proxy</code>&#x5B9E;&#x4F8B;&#x7684;&#x5BF9;&#x8C61;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> state = {
  _name: <span class="hljs-string">&apos;john&apos;</span>,
  name() {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._name
  }
}

<span class="hljs-keyword">let</span> pState = <span class="hljs-keyword">new</span> Proxy(state, {
  <span class="hljs-keyword">get</span>(target, key, receiver) {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;target[key]():&quot;</span>, target[key])
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;receiver !== pState:&quot;</span>, receiver !== pState)
    <span class="hljs-keyword">return</span> Reflect.get(target, key, receiver)
  }
})

<span class="hljs-keyword">const</span> son = {
  __proto__: pState,
  _name: <span class="hljs-string">&apos;son&apos;</span>
}

<span class="hljs-built_in">console</span>.log(son.name)
<span class="hljs-comment">// &#x56DE;&#x663E; target[key](): john</span>
<span class="hljs-comment">// &#x56DE;&#x663E; receiver !== pState: true</span>
<span class="hljs-comment">// &#x56DE;&#x663E; son</span>
</code></pre>
</li>
</ol>
<h3 id="&#x95EE;&#x9898;1&#x7684;&#x89E3;&#x6790;"><a name="&#x95EE;&#x9898;1&#x7684;&#x89E3;&#x6790;" class="anchor-navigation-ex-anchor" href="#&#x95EE;&#x9898;1&#x7684;&#x89E3;&#x6790;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.1. &#x95EE;&#x9898;1&#x7684;&#x89E3;&#x6790;</h3>
<p>&#x82E5;&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x8BFB;&#x64CD;&#x4F5C;&#x4E2D;&#x5C06;<code>Reflect.get(target, key, receiver)</code>&#x4FEE;&#x6539;&#x4E3A;<code>target[key]</code>&#x4F1A;&#x600E;&#x6837;&#x5462;&#xFF1F;
&#x5BF9;&#x4E8E;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x7684;&#x6570;&#x636E;&#x5C5E;&#x6027;(data property)&#x662F;&#x6CA1;&#x6709;&#x4EC0;&#x4E48;&#x5F71;&#x54CD;&#xFF0C;&#x4F46;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x7684;&#x8BBF;&#x95EE;&#x5668;&#x5C5E;&#x6027;(accessor property)&#x5219;&#x4F1A;&#x51FA;&#x73B0;&#x5982;&#x4E0B;&#x95EE;&#x9898;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> state = { 
  _value: <span class="hljs-number">1</span>, 
  get value() {
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>._value
  } 
}
<span class="hljs-keyword">const</span> reactiveState = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Proxy</span>(state, {
  get(target, key, receiver) {
    <span class="hljs-built_in">console</span>.log(key)
    <span class="hljs-keyword">return</span> target[key]
  }
})

reactiveState.value
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x53EA;&#x4F1A;&#x56DE;&#x663E;<code>value</code>&#xFF0C;&#x5373;&#x53EA;&#x662F;&#x62E6;&#x622A;&#x4E86;<code>value</code>&#x5C5E;&#x6027;&#x7684;&#x8BFB;&#x64CD;&#x4F5C;&#xFF0C;&#x5374;&#x6CA1;&#x6709;&#x62E6;&#x622A;<code>get value()</code>&#x5185;&#x8BBF;&#x95EE;&#x5C5E;&#x6027;<code>_value</code>&#x7684;&#x8BFB;&#x64CD;&#x4F5C;&#xFF0C;&#x90A3;&#x4E48;&#x5728;@vue/reactivity&#x4E2D;&#x5C31;&#x662F;&#x6CA1;&#x6709;&#x6536;&#x96C6;&#x5230;&#x5BF9;<code>_value</code>&#x7684;&#x4F9D;&#x8D56;&#x3002;
&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x901A;&#x8FC7;<code>target[key]</code>&#x8BBF;&#x95EE;<code>_value</code>&#x5C5E;&#x6027;&#x65F6;&#xFF0C;<code>target</code>&#x6307;&#x5411;&#x7684;&#x662F;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;<code>state</code>&#xFF0C;&#x800C;<code>receiver</code>&#x5219;&#x662F;&#x6307;&#x5411;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x3002;&#x6240;&#x4EE5;&#x5982;&#x679C;&#x6536;&#x96C6;&#x5BF9;<code>_value</code>&#x7684;&#x4F9D;&#x8D56;&#x5219;&#x9700;&#x8981;&#x901A;&#x8FC7;<code>Reflect.get(target, key, receiver)</code>&#x6765;&#x8BFB;&#x53D6;<code>_value</code>&#x7684;&#x503C;&#x3002;&#x6CE8;&#x610F;&#xFF1A;&#x8C03;&#x7528;<code>receiver[key]</code>&#x4F1A;&#x8FDB;&#x5165;&#x65E0;&#x9650;&#x9012;&#x5F52;&#x54E6;&#xFF01;</p>
<h3 id="&#x95EE;&#x9898;2&#x7684;&#x89E3;&#x6790;"><a name="&#x95EE;&#x9898;2&#x7684;&#x89E3;&#x6790;" class="anchor-navigation-ex-anchor" href="#&#x95EE;&#x9898;2&#x7684;&#x89E3;&#x6790;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.2. &#x95EE;&#x9898;2&#x7684;&#x89E3;&#x6790;</h3>
<p>&#x5728;&#x5199;&#x5165;&#x62E6;&#x622A;&#x65F6;&#xFF0C;&#x5982;&#x679C;<code>target === toRaw(receiver)</code>&#x6210;&#x7ACB;&#x5219;&#x89E6;&#x53D1;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x6267;&#x884C;&#x3002;
&#x800C;&#x8FD9;&#x91CC;&#x8981;&#x89E3;&#x51B3;&#x7684;&#x539F;&#x578B;&#x94FE;&#x7EE7;&#x627F;&#x95EE;&#x9898;&#x3002;
&#x9996;&#x5148;&#xFF0C;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x548C;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x516C;&#x7528;&#x4E00;&#x6761;&#x539F;&#x578B;&#x94FE;&#xFF0C;&#x4E14;&#x4E00;&#x65B9;&#x6539;&#x53D8;&#x539F;&#x578B;&#x94FE;&#x5C06;&#x5BFC;&#x81F4;&#x53E6;&#x4E00;&#x4E2A;&#x540C;&#x6B65;&#x6539;&#x53D8;&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> reactive = (state: object) =&gt; {
  <span class="hljs-keyword">const</span> RAW = <span class="hljs-string">&apos;__V_RAW&apos;</span>
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Proxy</span>(state, {
    get(target, key, receiver) {
      <span class="hljs-keyword">if</span> (key === RAW) {
        <span class="hljs-keyword">return</span> state.type
      }

      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;target.type&apos;</span>, target.type)
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;key&apos;</span>, key)
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;receiver.type&apos;</span>, receiver[RAW])

      <span class="hljs-keyword">return</span> <span class="hljs-built_in">Reflect</span>.get(target, key, receiver)
    }
  })
}

<span class="hljs-keyword">const</span> parent1 = { type: <span class="hljs-string">&apos;parent1&apos;</span>, value: <span class="hljs-number">1</span>}
<span class="hljs-keyword">const</span> parent2 = { type: <span class="hljs-string">&apos;parent2&apos;</span>, value: <span class="hljs-number">2</span>}
<span class="hljs-keyword">const</span> child = { type: <span class="hljs-string">&apos;child&apos;</span> }

<span class="hljs-keyword">const</span> pParent1 = reactive(parent1)
<span class="hljs-keyword">const</span> pChild = reactive(child)

<span class="hljs-built_in">Object</span>.setPrototypeOf(pChild, pParent1)
pChild.value
<span class="hljs-comment">/**
 * &#x56DE;&#x663E;&#x5982;&#x4E0B;
 * target.type child
 * key value
 * receiver.type child
 * target.type parent1
 * key value
 * receiver.type child
 */</span>
<span class="hljs-built_in">Object</span>.setPrototypeOf(child, pParent2)
pChild.value
<span class="hljs-comment">/**
 * &#x56DE;&#x663E;&#x5982;&#x4E0B;
 * target.type child
 * key value
 * receiver.type child
 * target.type parent2
 * key value
 * receiver.type child
 */</span>
</code></pre>
<p>&#x4ECE;&#x4E0A;&#x9762;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x65E0;&#x8BBA;&#x662F;&#x4FEE;&#x6539;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x8FD8;&#x662F;&#x88AB;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x7684;&#x539F;&#x578B;&#x94FE;&#xFF0C;&#x4E24;&#x8005;&#x7684;&#x539F;&#x578B;&#x94FE;&#x5747;&#x65F6;&#x523B;&#x4FDD;&#x6301;&#x540C;&#x6B65;&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5F53;&#x8BBF;&#x95EE;&#x5B50;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x6CA1;&#x6709;&#x7684;&#x5C5E;&#x6027;&#x65F6;&#x4F1A;&#x8BBF;&#x95EE;&#x5176;&#x539F;&#x578B;&#x94FE;&#x7684;&#x5BF9;&#x8C61;&#x67E5;&#x627E;&#x5C5E;&#x6027;&#xFF0C;&#x5E76;&#x4E14;<code>get</code>&#x62E6;&#x622A;&#x5668;&#x7684;<code>receiver</code>&#x4FDD;&#x6301;&#x6307;&#x5411;&#x5B50;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x3002;
&#x90A3;&#x4E48;&#x901A;&#x8FC7;<code>effect(() =&gt; { console.log(pChild.value) })</code>&#x8BBF;&#x95EE;&#x54CD;&#x5E94;&#x5F0F;&#x5BF9;&#x8C61;&#x65F6;&#x526F;&#x4F5C;&#x7528;&#x51FD;&#x6570;&#x5C06;&#x4E0D;&#x4EC5;&#x4EC5;&#x4F9D;&#x8D56;<code>pChild.value</code>&#xFF0C;&#x8FD8;&#x610F;&#x5916;&#x4F9D;&#x8D56;&#x4E86;<code>pParent.value</code>&#x3002;&#x90A3;&#x4E48;&#x6709;&#x4EC0;&#x4E48;&#x529E;&#x6CD5;&#x5904;&#x7406;&#x5462;&#xFF1F;
&#x65E2;&#x7136;&#x7236;&#x4EE3;&#x7406;<code>get</code>&#x62E6;&#x622A;&#x5668;&#x7684;<code>receiver</code>&#x4F9D;&#x7136;&#x6307;&#x5411;&#x5B50;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#xFF0C;&#x90A3;&#x4E48;&#x81EA;&#x7136;&#x800C;&#x8A00;&#x901A;&#x8FC7;&#x5224;&#x65AD;<code>toRaw(receiver)</code>&#x662F;&#x5426;&#x7B49;&#x4E8E;<code>target</code>&#x5373;&#x53EF;&#x4E86;&#x3002;</p>
<h3 id="&#x95EE;&#x9898;3&#x7684;&#x89E3;&#x6790;"><a name="&#x95EE;&#x9898;3&#x7684;&#x89E3;&#x6790;" class="anchor-navigation-ex-anchor" href="#&#x95EE;&#x9898;3&#x7684;&#x89E3;&#x6790;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3.3. &#x95EE;&#x9898;3&#x7684;&#x89E3;&#x6790;</h3>
<p>&#x53E6;&#x5916;&#xFF0C;&#x5728;&#x5F00;&#x7BC7;<a href="../1_online_rendering/1_start_from_static_view.html">&#x300A;&#x4ECE;&#x9759;&#x6001;&#x89C6;&#x56FE;&#x9996;&#x6B21;&#x6E32;&#x67D3;&#x5F00;&#x59CB;&#x300B;</a>&#x4E2D;&#x521B;&#x5EFA;&#x4F5C;&#x7528;&#x57DF;&#x94FE;<code>createScopedContext</code>&#x5982;&#x4E0B;&#x4EE3;&#x7801;
<code>receiver === reactiveProxy &amp;&amp; !target.hasOwnProperty(key)</code>&#x5373;&#x5BF9;&#x5F53;&#x524D;&#x4F5C;&#x7528;&#x57DF;(<code>receiver === reactiveProxy</code>)&#x8FDB;&#x884C;&#x5199;&#x64CD;&#x4F5C;&#x65F6;&#xFF0C;&#x82E5;&#x5C5E;&#x6027;&#x4E0D;&#x5B58;&#x5728;&#x4E8E;&#x8BE5;&#x4F5C;&#x7528;&#x57DF;&#x5BF9;&#x8C61;&#xFF0C;&#x5219;&#x5F80;&#x7236;&#x4F5C;&#x7528;&#x57DF;&#x4E0A;&#x9012;&#x5F52;&#x6267;&#x884C;&#x5199;&#x64CD;&#x4F5C;&#x3002;</p>
<script type="text/javascript">
  /**
   * Donation
   */
  function showQR(){
    var rewardButton = document.getElementById('rewardButton')
    var qr = document.getElementById('QR')
    if (rewardButton) {
      rewardButton.style.display = 'none'
    }
    if (qr) {
      qr.style.display = 'block'
    }
  }
  setInterval(showQR, 1000)

  /**
   * Baidu analysis
   */
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?8bca1e3aa362a7f85dbe8304cff65225";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
</script>


<footer class="page-footer"><span class="copyright">Copyright &#xA9; fsjohnhuang 2022 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">File Modify: 
2022-05-05 09:19:15
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="0_intro.html" class="navigation navigation-prev " aria-label="Previous page: 章节介绍">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="2_reactive_map_set.html" class="navigation navigation-next " aria-label="Next page: 逐行解读reactive如何构建Map和Set的响应式对象">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"逐行解读reactive如何构建对象和数组的响应式对象","level":"3.2","depth":1,"next":{"title":"逐行解读reactive如何构建Map和Set的响应式对象","level":"3.3","depth":1,"path":"2_reactivity_system/2_reactive_map_set.md","ref":"2_reactivity_system/2_reactive_map_set.md","articles":[]},"previous":{"title":"章节介绍","level":"3.1","depth":1,"path":"2_reactivity_system/0_intro.md","ref":"2_reactivity_system/0_intro.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","-sharing","theme-comscore","search-pro","code","back-to-top-button","tbfed-pagefooter","chapter-fold","expandable-chapters","splitter","anchor-navigation-ex","donate","sharing-plus","baidu"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy fsjohnhuang 2022"},"chapter-fold":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"code":{"copyButtons":true},"donate":{"alipay":"","alipayText":"支付宝打赏","button":"赞赏","title":"","wechat":"/petite-vue-source-reading/images/wechat.jpg","wechatText":"微信打赏"},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":true},"theme-comscore":{},"back-to-top-button":{},"baidu":{"token":"8bca1e3aa362a7f85dbe8304cff65225"},"sharing":{"qq":false,"all":["qq","qzone","weibo","whatsapp","twitter","facebook","line"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"^_^肥仔John","lang":"zh-cn","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"","variables":{},"title":"petite-vue源码剖析","gitbook":"*","description":""},"file":{"path":"2_reactivity_system/1_reactive_object_array.md","mtime":"2022-05-05T01:19:15.370Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-05T01:20:11.998Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-donate/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-baidu/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

